* build_mw_measures.do	JEP 01/23/15
* This file builds on prelim_est*.do, builds a program to generate the minimum wage measures.

* 1. Choose a sample and a measure
* 2. Calculate minimum wage measures
* 3. Create treatment variables and trends
* 4. Run regressions

*************************** Change log ********************************** 

/*
	01/24/15	JEP		Modify workflow so measure building occurs after the sample choice, buid_mw_measures only does a processing part.
						Take building of city specific trends and mw leads and lags to build_var_ocupados, since they are sample independent.
						Modify to work with a specific sample, that way I don't have to drop obs and work with a different dataset every time.
	03/06/15	JEP		Add nominal measures to see if it changes much
						Take the rounding of reporting into account
	04/08/15	JEP 		Add calculation of bites by city-industry: bite ind
	04/16/15	JEP		Adjust bite calculations for cases when the real minimum wage is not increasing, this is neccesary for the placebo tests
	
	09/26/18	JEP		Add calculation of bites by industry only (not city). These would go with industry FE.
	
*/
						
****** 2. Calculate minimum wage measures

cap program drop mw_measures
program mw_measures
	syntax varname, stub(string) [time(string)]
	
	if "`time'"=="" loc time tq(1998q4)
	
	* 2.1 Minimum wage bite
	* Bite has to be percentage of people earning between the old real minimum wage and the new real minimum wage each period.
	* Do I use the per city real minimum wage or the countrywide one?
	* Use the per-city, to be net of differential inflation

	* Bite in t: % of people earning above minw in t and below minw in t+1
	* If the real minimum wage falls, then the bite is the percentage people above minw t+1 and below minw in t
	* But then the correlation between the bite and avg minimum wages would be negative over that period (larger bites, larger avg falls)
	* Take bite only for the 1998 end of year increase. This would be equivalent to the 0/1 classification at that point if I were running a standard diff in diff

	* Create several "bite" measures

	* Affected variables 
		
	gen af_bite_`stub'=0
	gen af_bite_e_`stub'=0
	gen af_bite_fb_`stub'=0
	
	* 1. Just bite
	* To rescale for interpretation, bites are multiplied by 100, so af is 100 or 0. This way I don't need to rescale the bites afterwards.
	* If real mw is increasing
	replace af_bite_`stub'=100 if salario_mensual_real_p>=minw_mensual_real_p & salario_mensual_real_p<=minw_mensual_real_p_f1 & minw_mensual_real_p<=minw_mensual_real_p_f1
	* If real mw is decreasing
	replace af_bite_`stub'=100 if af_bite_`stub'==0 & salario_mensual_real_p<=minw_mensual_real_p & salario_mensual_real_p>=minw_mensual_real_p_f1 & minw_mensual_real_p>=minw_mensual_real_p_f1
	* 2. Extended bite (incidencia modal)
	replace af_bite_e_`stub'=100 if salario_mensual_real_p>=0.9*minw_mensual_real_p & salario_mensual_real_p<=1.1*minw_mensual_real_p_f1 & minw_mensual_real_p<=minw_mensual_real_p_f1
	replace af_bite_e_`stub'=100 if af_bite_e_`stub'==0 & salario_mensual_real_p<=0.9*minw_mensual_real_p & salario_mensual_real_p<=1.1*minw_mensual_real_p_f1 & minw_mensual_real_p>=minw_mensual_real_p_f1
	* 3. Fraction below minimum
	replace af_bite_fb_`stub'=100 if salario_mensual_real_p<=minw_mensual_real_p_f1 & minw_mensual_real_p<=minw_mensual_real_p_f1
	replace af_bite_fb_`stub'=100 if af_bite_fb_`stub'==0 & salario_mensual_real_p>=minw_mensual_real_p_f1 & minw_mensual_real_p>=minw_mensual_real_p_f1
	
	* Nominal bites
	
	gen af_nom_bite_`stub'=0
	gen af_nom_bite_e_`stub'=0
	gen af_nom_bite_fb_`stub'=0
	
	* 1. Just bite
	* To rescale for interpretation, bites are multiplied by 100, so af is 100 or 0. This way I don't need to rescale the bites afterwards.
	replace af_nom_bite_`stub'=100 if salario_mensual>=minw & salario_mensual<=minw_f1 & minw<=minw_f1
	* 2. Extended bite (incidencia modal)
	replace af_nom_bite_e_`stub'=100 if salario_mensual>=0.9*minw & salario_mensual<=1.1*minw_f1 & minw<=minw_f1
	* 3. Fraction below minimum
	replace af_nom_bite_fb_`stub'=100 if salario_mensual<=minw_f1 & minw<=minw_f1
	
	
	* Nominal bites, accounting for rounding	
	
	gen af_nom_round_bite_`stub'=0
	gen af_nom_round_bite_e_`stub'=0
	gen af_nom_round_bite_fb_`stub'=0
	
	replace af_nom_round_bite_`stub'=100 if salario_mensual_round>=minw_round & salario_mensual_round<=minw_round_f1 & minw_round<=minw_round_f1
	replace af_nom_round_bite_`stub'=100 if af_nom_round_bite_`stub'==0 & salario_mensual_round<=minw_round & salario_mensual_round>=minw_round_f1 & minw_round>=minw_round_f1
	* 2. Extended bite (incidencia modal)
	replace af_nom_round_bite_e_`stub'=100 if salario_mensual_round>=0.9*minw_round & salario_mensual_round<=1.1*minw_round_f1 & minw_round<=minw_round_f1
	replace af_nom_round_bite_e_`stub'=100 if af_nom_round_bite_e_`stub'==0 & salario_mensual_round<=0.9*minw_round & salario_mensual_round>=1.1*minw_round_f1 & minw_round>=minw_round_f1
	* 3. Fraction below minimum
	replace af_nom_round_bite_fb_`stub'=100 if salario_mensual_round<=minw_round_f1 & minw_round<=minw_round_f1
	replace af_nom_round_bite_fb_`stub'=100 if af_nom_round_bite_fb_`stub'==0 & salario_mensual_round>=minw_round_f1 & minw_round>=minw_round_f1
	
	
	
	/* tabulate ciudad time, summ(af) means */
	
	* Make list of variables to collapse
	glo to_coll ""
	glo to_collw ""
	foreach pre in _ _nom_ _nom_round_ {
		foreach post in _ _e_ _fb_	{
			glo to_coll "$to_coll `pre'bite`post'`stub'=af`pre'bite`post'`stub'"
			glo to_collw "$to_collw `pre'bite`post'`stub'=af`pre'bite`post'`stub'"
		}
	}
	
	
	
	* Collapse by mean gives the percentage affected
	* By city
	preserve
	collapse (mean) ${to_coll} if `1', by(ccode time)
	rename _* *
	* Keep the bites right before the MW jump
	xtset ccode time
	keep if time==`time'
	tempfile bit
	save `bit'
	restore
	
	
	* By city industry
	preserve
	collapse (mean) ${to_coll} if `1', by(ccode cactividad_empresa time)
	rename _* *
	* Keep the bites right before the MW jump
	keep if time==`time'
	rename *bite* *bite*_ind
	tempfile bitind
	save `bitind'
	restore
	
	
	
	
	* By industry only
	preserve
	collapse (mean) ${to_coll} if `1', by(cactividad_empresa time)
	rename _* *
	* Keep the bites right before the MW jump
	keep if time==`time'
	tempfile bitindo
	rename *bite* *bite*_indo
	save `bitindo'
	restore
	
	merge n:1 ccode using `bit'
	drop _merge
	
	
	merge n:1 ccode cactividad_empresa using `bitind'
	drop _merge
	
	
	
	merge n:1 cactividad_empresa using `bitindo'
	drop _merge
	
	
	
	

	* Generate weighted bite measures (not used)
	
	* By city
	preserve
	collapse (mean)${to_collw} if `1' [iw=fex], by(ccode time)
	rename _* *
	xtset ccode time
	keep if time==`time'
	tempfile bit
	save `bit'
	restore
	
	* By city industry
	preserve
	collapse (mean)${to_collw} if `1' [iw=fex], by(ccode cactividad_empresa time)
	rename _* *
	keep if time==`time'
	rename *bite* *bite*_ind
	tempfile bitind
	save `bitind'
	restore
	
	* By industry only
	preserve
	collapse (mean)${to_collw} if `1' [iw=fex], by(cactividad_empresa time)
	rename _* *
	keep if time==`time'
	rename *bite* *bite*_indo
	tempfile bitindo
	save `bitindo'
	restore
	
	merge n:1 ccode using `bit'
	drop _merge

	merge n:1 ccode cactividad_empresa using `bitind'
	drop _merge
	merge n:1 cactividad_empresa using `bitindo'
	drop _merge
	
	
	
	* 2.2 Minimum to median
	* To make it forward looking as the bite, it has to be the next mw relative to the median
	egen salario_real_mensual_med_`stub' = median(salario_mensual_real_p) if `1', by(ccode cactividad_empresa time)
	gen mw_to_medw_`stub' = minw_mensual_real_p_f1/salario_real_mensual_med if `1'
	levelsof ccode, local(c)
	levelsof cactividad_empresa, local(a)
	foreach city in `c' {
		foreach aa in `a' {
			sum mw_to_medw_`stub' if time==`time' & ccode==`city' & cactividad==`aa' & `1', meanonly
			replace mw_to_medw_`stub'=r(mean) if ccode==`city' & cactividad==`aa' & `1'
		}
	}

	/* tabulate ciudad time, summ(mw_to_medw) means */

	/* * How correlated are the measures?
	preserve
	collapse (firstnm) bite mw_to_medw, by(ciudad time)
	bys ciudad: corr bite mw_to_medw
	line bite mw_to_medw time if ciudad=="BOGOTÁ"
	restore */

	
	
	
end

* save "`master'/Data/Source/ocupados_mw_bites.dta", replace

* Project output
*project, creates("`master'/Data/Source/ocupados_mw_bites.dta")


